﻿// <copyright file="SortableObservableCollection.cs" company="XamlNinja">
//   2011 Richard Griffin and Ollie Riches
// </copyright>
// <summary>
//   Thread safe sortable observable collection...
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WP7Contrib.Collections
{
    using System;
    using System.Collections.Generic;
    using System.Collections.Specialized;

    /// <summary>
    /// Thread safe sortable observable collection - warning sorting the collection cause the collection raised to reset
    /// any bound control, use with caution.
    /// </summary>
    /// <typeparam name="T">The type being used for binding.
    /// </typeparam>
    public sealed class SortableObservableCollection<T> : ObservableCollection<T>
    {
        public void Sort()
        {
            this.collection.Sort();
        }

        public void Sort(IComparer<T> comparer)
        {
            lock (sync)
            {
                this.collection.Sort(comparer);
            }
            this.RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
        }

        public void Sort(int index, int count, IComparer<T> comparer)
        {
            lock (sync)
            {
                this.collection.Sort(index, count, comparer);
            }
            this.RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
        }

        public void Sort(Comparison<T> comparison)
        {
            lock (sync)
            {
                this.collection.Sort(comparison);
            }
            this.RaiseCollectionChanged(NotifyCollectionChangedAction.Reset);
        }
    }
}